Picturing Electromagnetic Waves Traveling Along a Diagonal Axis

  • PROGRAM: Picturing electromagnetic waves traveling along a diagonal axis
  • CREATED: 6/4/2018

In [16]:
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import axes3d

In [17]:
#Define constants - speed of light, frequency, electric field amplitude, wave number. 
c = 2.99792 * 10**8
w = 4
E_0 = 2
k = 1

In [18]:
#Define a function for the electric field.
def E(x, y, z, t):
    return 1/np.sqrt(2) * E_0 * np.cos(k/np.sqrt(3) * (x + y + z) - w*t)

In [19]:
#Define a function for the magnetic field.
def B(x, y, z, t):
    return 1/(c * np.sqrt(2)) * E_0 * np.cos(k/np.sqrt(3) * (x + y + z) - w*t)

In [35]:
#Although this is a plane wave, plot the wave along a single line along the vector k = (1, 1, 1).
fig = plt.figure(figsize = (10, 10))
ax = fig.gca(projection = '3d')

#Choose a time (in seconds) to plot the electric and magnetic fields.
t = 0

#Make a grid of points where vectors of the vector field are placed.
x_lim = 10
y_lim = 10
z_lim = 10

X = np.arange(-x_lim, x_lim, 0.2)
Y = np.arange(-y_lim, y_lim, 0.2)
Z = np.arange(-z_lim, z_lim, 0.2)

U_electric = E(X, Y, Z, t)
V_electric = 0
W_electric = -E(X, Y, Z, t)
U_magnetic = B(X, Y, Z, t) * c
V_magnetic = -2 * B(X, Y, Z, t) * c
W_magnetic = B(X, Y, Z, t) * c

#Plot the vector field - electric field.
ax.quiver(X, Y, Z, U_electric, V_electric, W_electric, color = 'orange', arrow_length_ratio = 0.03, linewidths = 1, label = 'Electric Field')
#Plot the vector field - magnetic field.
ax.quiver(X, Y, Z, U_magnetic, V_magnetic, W_magnetic, color = 'blue', arrow_length_ratio = 0.03, linewidths = 1, label = 'Magnetic Field')

#Adjust plot size.
ax.set_xlim3d(-10, 10)
ax.set_ylim3d(-10, 10)
ax.set_zlim3d(-10, 10)

#Adjust the viewing angle of the plot.
ax.view_init(elev = 30, azim = 290)

#Label the plot.
ax.set_xlabel('x (meters)')
ax.set_ylabel('y (meters)')
ax.set_zlabel('z (meters)')
ax.set_title('Electric and Magnetic Fields of Electromagnetic Wave \n Traveling Along Diagonal Axis', fontsize = 16)

ax.legend(loc = 'lower left')

plt.show()